//
//  UserWeiboController.m
//  collection
//
//  Created by 靖 张 on 12/12/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "UserWeiboTableHandler.h"
#import "FeedDetailController.h"
#import "FeedCell.h"

@interface UserWeiboTableHandler() 


@end

@implementation UserWeiboTableHandler

- (void)dealloc {
    self.homepageRootController = nil;
    self.dataList = nil;
    
    [super dealloc];
}

- (void)setDataList:(NSArray *)dataList {

    // Filter the list
    NSMutableArray* array = [[NSMutableArray alloc] init];
    for (Feed* oneFeed in dataList) {
        if (oneFeed.userID == self.userID) {
            [array addObject:oneFeed];
        }
    }
    
    super.dataList = array;
    [array release];
    
    NSLog(@"Set data list");
}

#pragma mark - Implementation of UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.dataList count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString* latestInfoCellID = @"LatestInfoCell";
    FeedCell* cell = (FeedCell*)[tableView dequeueReusableCellWithIdentifier:latestInfoCellID];
    if (cell == nil) {
        cell = [[[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:latestInfoCellID] autorelease];
    }
    
    // Customise the cell
    Feed* feed = [self.dataList objectAtIndex:indexPath.row];
    [cell updateCell:feed delegate:nil];
    
    return cell;
}

#pragma mark - Implementation of UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

    return cell.frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.homepageRootController != nil && 
        self.homepageRootController.navigationController != nil) {
        FeedDetailController* feedDetailController = [[FeedDetailController alloc] initWithNibName:@"FeedDetailController" bundle:nil];
        Feed* feed = [self.dataList objectAtIndex:indexPath.row];
        feedDetailController.feed = feed;
        [self.homepageRootController.navigationController pushViewController:feedDetailController animated:YES];
        [feedDetailController release];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
